<?php
namespace Common\Model;

class MenuModel {
    private $dao = array();
    private $perIdLen = 2;   //每级编号的长度

    /**
     * setDao
     * 设置菜单数据源
     *
     * @param mixed $dao
     * @return void
     */
    public function setDao($dao) {
        $this->dao = $dao;
    }

    /**
     * getByPid
     * 根据父编号返回所有直属子菜单，不包含子菜单的下级菜单
     *
     * @param mixed $pid
     * @return void
     */
    public function getByPid($pid = 0) {
        $pid        = (int)$pid;
        $pidLen     = empty($pid) ? 0 : strlen($pid);
        $childIdLen = $pidLen + $this->perIdLen;

        $r = array();

        foreach($this->dao as $v) {
            if(!empty($pid) && substr($v['id'], 0, $pidLen) != $pid) {
                continue;
            }

            if($childIdLen != strlen($v['id'])) {
                continue;
            }

            $r[] = $v;
        }

        return $r;
    }

}
